package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebCooperaterDao;
import ysq.xyg.web.model.po.WebCooperater;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebCooperaterVo;
import ysq.xyg.web.service.WebCooperaterService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 网站模块-合作信息service实现
 *
 * @author shengQiang yu
 * @date 2019-9-6 15:43:24
 */
@Service("webCooperaterService")
public class WebCooperaterServiceImpl extends BaseServiceImpl<WebCooperater> implements WebCooperaterService {

    @Resource
    private WebCooperaterDao webCooperaterDao;

    private WebCooperater webCooperater;

    @Autowired
    public void setBaseDao(WebCooperaterDao webCooperaterDao) {
        this.baseDao = webCooperaterDao;
    }


    /**
     * 分页查询
     *
     * @param webCooperaterVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-6 15:43:24
     */
    public ReturnEntity page(WebCooperaterVo webCooperaterVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebCooperater> list = acceptList(webCooperaterVo);
        PageInfo<WebCooperater> page=new PageInfo<WebCooperater>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webCooperaterVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-6 15:43:24
     */
    public ReturnEntity addOrUpdate(WebCooperaterVo webCooperaterVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webCooperaterVo.getId())){
            webCooperater=new WebCooperater(currentUser);
            isUpate=false;
        }else{
            webCooperater=selectByPrimaryKey(webCooperaterVo.getId());
            if(ObjectUtil.isNull(webCooperater)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webCooperater.setUpdateTime(new Date());
            webCooperater.setUpdateUser(currentUser.getId());
        }
        webCooperater.setCompany(webCooperaterVo.getCompany());
        webCooperater.setName(webCooperaterVo.getName());
        webCooperater.setPhone(webCooperaterVo.getPhone());
        webCooperater.setEmail(webCooperaterVo.getEmail());
        webCooperater.setRemark(webCooperaterVo.getRemark());
        webCooperater.setOperateStatus(webCooperaterVo.getOperateStatus());
        webCooperater.setUserId(webCooperaterVo.getUserId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webCooperater);
        }else{
            num=insert(webCooperater);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-6 15:43:24
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebCooperater webCooperater=selectByPrimaryKey(id);
        if(webCooperater!=null){
            webCooperater.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webCooperater.setUpdateTime(new Date());
            webCooperater.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webCooperater);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webCooperaterVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-9-6 15:43:24
     */
    public List<WebCooperater> acceptList(WebCooperaterVo webCooperaterVo){
        Example example = new Example(WebCooperater.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webCooperaterVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webCooperaterVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webCooperaterDao.selectByExample(example);
    }
}
